package com.wangtao.firstapp.activity.stairactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.MainActivity;
import com.wangtao.firstapp.activity.PhotoPickActivity;
import com.wangtao.firstapp.activity.RelationProductActivity;
import com.wangtao.firstapp.activity.TakePhotoActivity;

/**
 * 写文章页面
 * Created by lanouhn on 16/8/28.
 */
public class WriteArticleActivity extends Activity {
    private ImageButton iv_title_bar_back;// 标题栏返回按钮
    private TextView tv_preview_writearticle;// 标题栏  预览
    private TextView tv_publish_writeartilce;// 标题栏  发布按钮
    private EditText edittext_title_writearticle;// 文章标题
    private EditText edittext_content_writearticle;// 文章正文
    private LinearLayout linearlayout;
    private LinearLayout linearlayout_image;
    private LinearLayout linearlayout_product;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_writearticle);
        initView();
        initEvent();
    }

    private void initView() {
        iv_title_bar_back = (ImageButton) findViewById(R.id.iv_title_bar_back);
        tv_preview_writearticle = (TextView) findViewById(R.id.tv_preview_writearticle);
        tv_publish_writeartilce = (TextView) findViewById(R.id.tv_publish_writeartilce);
        edittext_title_writearticle = (EditText) findViewById(R.id.edittext_title_writearticle);
        edittext_content_writearticle = (EditText) findViewById(R.id.edittext_content_writearticle);
        linearlayout = (LinearLayout) findViewById(R.id.linearlayout);
        linearlayout_image = (LinearLayout) findViewById(R.id.linearlayout_image);
        linearlayout_product = (LinearLayout) findViewById(R.id.linearlayout_product);

        TextChange textChange = new TextChange();
        edittext_title_writearticle.addTextChangedListener(textChange);
        edittext_content_writearticle.addTextChangedListener(textChange);
    }

    private void initEvent() {
        // 标题栏返回按钮点击事件
        iv_title_bar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 当正文部分获取到焦点时弹出底部 选取图片和产品
        edittext_content_writearticle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    linearlayout.setVisibility(View.VISIBLE);
                } else {
                    linearlayout.setVisibility(View.GONE);
                }
            }
        });
        // 底部选取图片  链接商品  的点击事件
        linearlayout_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WriteArticleActivity.this, PhotoPickActivity.class);
                startActivityForResult(intent, 2);
            }
        });
        linearlayout_product.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WriteArticleActivity.this, RelationProductActivity.class);
                startActivityForResult(intent, 2);
            }
        });
    }

    // 当标题和正文都不为空时  预览 发布  变色
    public class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (edittext_title_writearticle.length() > 0 && edittext_content_writearticle.length() > 0) {
                tv_preview_writearticle.setClickable(true);
                tv_preview_writearticle.setTextColor(getResources().getColor(R.color.mycolor2));
                tv_publish_writeartilce.setClickable(true);
                tv_publish_writeartilce.setTextColor(getResources().getColor(R.color.mycolor1));
            }
            if (edittext_title_writearticle.length() == 0 || edittext_content_writearticle.length() == 0) {
                tv_preview_writearticle.setClickable(false);
                tv_preview_writearticle.setTextColor(getResources().getColor(R.color.mycolor3));
                tv_publish_writeartilce.setClickable(false);
                tv_publish_writeartilce.setTextColor(getResources().getColor(R.color.mycolor2));
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}
